<?php
/**
 * 接口配置
 * Enter description here ...
 * @author jin
 *
 */
class ControllerConfigBussness extends Controller {
	private $error = array();

	protected function init(){
		$this->load_language('config/bussness');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('config/bussness');
	}

	public function index() {
		$this->init();
		$this->getList();
	}

	public function insert() {
		$this->init();

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_config_bussness->addBussness($this->request->post);

			$this->redirectToList();
		}

		$this->getForm();
	}

	public function update() {
		$this->init();

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_config_bussness->editBussness($this->request->get['bussness_type_id'], $this->request->post);

			$this->redirectToList();
		}

		$this->getForm();
	}

	public function delete() {
		$this->init();

		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $postion_id) {
				$this->model_config_bussness->deleteBussness($postion_id);
			}
			$this->redirectToList();
		}

		$this->getList();
	}
	 
	 
	private function getList() {
		
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}

		$data = array(
			'start' => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit' => $this->config->get('config_admin_limit')
		);


		$this->data['bussnesss'] = array();

		$user_total = $this->model_config_bussness->getTotalBussnesss();

		$results = $this->model_config_bussness->getBussnesss($data);

		foreach ($results as $result) {
			$action = array();

			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'type' => "edit",
				'href' => $this->url->link('config/bussness/update', 'token=' . $this->session->data['token'] . '&bussness_type_id=' . $result['bussness_type_id'] , 'SSL')
			);


			$this->data['bussnesss'][] = array(
				'bussness_type_id'    => $result['bussness_type_id'],
      			'type_name'    => $result['type_name'],
				'code'    => $result['code'],
				'actions'     => $action,
      			'selected'   => isset($this->request->post['selected']) && in_array($result['bussness_type_id'], $this->request->post['selected']),
			);
		}

		$url = "";

		if(isset($this->request->get['bussness_type_id']))
		{
			$url = "&bussness_type_id="+$this->request->get['bussness_type_id'];
		}


		$this->data['insert'] = $this->url->link('config/bussness/insert', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['delete'] = $this->url->link('config/bussness/delete', 'token=' . $this->session->data['token'].$url, 'SSL');
		
		$this->data['token'] = $this->session->data['token'];
		$pagination = new Pagination();
		$pagination->total = $user_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('config/bussness', 'token=' . $this->session->data['token'] . '&page={page}', 'SSL');
		$this->data['pagination'] = $pagination->render();
		
		$this->template = 'config/config_bussness_list.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
		$this->response->setOutput($this->render());
	}

	private function getForm() {
		
		
		if (isset($this->error['title'])) {
			$this->data['error_title'] = $this->error['title'];
		} else {
			$this->data['error_title'] = '';
		}

		if (isset($this->error['error_date'])) {
			$this->data['error_date'] = $this->error['error_date'];
		} else {
			$this->data['error_date'] = '';
		}

		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

		$url=$this->getUrlParameters();

		$this->data['breadcrumbs'] = array();

		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
		);

		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('config/bussness', 'token=' . $this->session->data['token'] , 'SSL'),
      		'separator' => ' :: '
      		);

      		if (!isset($this->request->get['bussness_type_id'])) {
      			$this->data['action'] = HTTPS_SERVER . 'index.php?route=config/bussness/insert&token=' . $this->session->data['token'] . $url;
      		} else {
      			$this->data['action'] = HTTPS_SERVER . 'index.php?route=config/bussness/update&token=' . $this->session->data['token'] . '&bussness_type_id=' . $this->request->get['bussness_type_id'] . $url;
      		}

      		$this->data['cancel'] = HTTPS_SERVER . 'index.php?route=config/bussness&token=' . $this->session->data['token'] . $url;

      		$this->data['token'] = $this->session->data['token'];

      		if (isset($this->request->get['bussness_type_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
      			$bussness_info = $this->model_config_bussness->getBussness($this->request->get['bussness_type_id']);
      		}
      		
      		
			if (isset($this->request->post['type_name'])) {
				$this->data['type_name'] = $this->request->post['type_name'];
			} elseif (!empty($bussness_info)) {
				$this->data['type_name'] = $bussness_info['type_name'];
			} else {
				$this->data['type_name'] = '';
			}
			
			if (isset($this->request->post['code'])) {
				$this->data['code'] = $this->request->post['code'];
			} elseif (!empty($bussness_info)) {
				$this->data['code'] = $bussness_info['code'];
			} else {
				$this->data['code'] = '';
			}
			
		   if (isset($this->request->post['description'])) {
				$this->data['description'] = $this->request->post['description'];
			} elseif (!empty($bussness_info)) {
				$this->data['description'] = $bussness_info['description'];
			} else {
				$this->data['description'] = '';
			}
      		
      		$this->template = 'config/config_bussness_form.tpl';
			$this->children = array(
				'common/header',
				'common/footer'
			);
			$this->response->setOutput($this->render());
      		
	}
	

	private function validateForm() {
		/*
		if (!$this->user->hasPermission('modify', 'catalog/article')) {
			$this->error['warning'] = sprintf($this->language->get('error_permission'),$this->language->get('heading_title'));
		}

		foreach ($this->request->post['article'] as $language_id => $value) {
			if ((strlen(utf8_decode($value['title'])) < 1)) {
				$this->error['title'][$language_id] = $this->language->get('error_name');
			}
		}
		 */
		if (!$this->error) {
			return TRUE;
		} else {
			$this->error['warning'] = $this->language->get('error_required_data');
				
			return FALSE;
		}
	}

	private function validateDelete() {
		/*
		if (!$this->user->hasPermission('modify', 'catalog/article')) {
			$this->error['warning'] = sprintf($this->language->get('error_permission'),$this->language->get('heading_title'));
		}
		*/

		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}
	}


	private function redirectToList(){
		$this->session->data['success'] = sprintf($this->language->get('text_success'),$this->language->get('heading_title'));

		$url=$this->getUrlParameters();

		$this->redirect(HTTPS_SERVER . 'index.php?route=config/bussness&token=' . $this->session->data['token'] . $url);
	}
	 
	private function getCommonUrlParameters(){
		$url = '';

		if (isset($this->request->get['filter_title'])) {
			$url .= '&filter_title=' . $this->request->get['filter_title'];
		}

		if (isset($this->request->get['filter_status'])) {
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		}

		if (isset($this->request->get['filter_date_add'])) {
			$url .= '&filter_date_add=' . $this->request->get['filter_date_add'];
		}

		if (isset($this->request->get['filter_article_category_id'])) {
			$url .= '&filter_article_category_id=' . $this->request->get['filter_article_category_id'];
		}

		return $url;
	}
	 
	public function getUrlParameters(){
		$url = '';

		$url=$this->getCommonUrlParameters();

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		return $url;
	}

}
?>